/*
 * StartApplication.java
 *
 * Created on May 30, 2008 11:02:31 PM;
 */

package org.gunncs.deploy;


import org.gunncs.actoriface.*;
import com.iaroc.irobot.IRobotCreate;
import com.iaroc.irobot.util.*;
import com.sun.spot.peripheral.Spot;
import com.sun.spot.sensorboard.EDemoBoard;
import com.sun.spot.sensorboard.peripheral.LEDColor;
import com.sun.spot.sensorboard.peripheral.ITriColorLED;
import com.sun.spot.util.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import com.sun.spot.sensorboard.peripheral.Servo;
import org.gunncs.daemons.*;
import org.gunncs.daemons.telemetry.*;
/**
 * The startApp method of this class is called by the VM to start the
 * application.
 * 
 * The manifest specifies this class as MIDlet-1, which means it will
 * be selected for execution.
 *
 * Kernel and Daemon Threads are started from here, and return here 
 * on panic.
 * @author  ajc
 */
public class StartApplication extends MIDlet {
   
    Robot bot;
    
    //First LED shows MIDLET status
    ITriColorLED overallStatus = EDemoBoard.getInstance().getLEDs()[0];
    
    protected void startApp() throws MIDletStateChangeException {
        
        /** monitor the USB (if connected) and recognize commands from host
         *  This allows us to control and debug between restarts
         */
        new BootloaderListener().start();
        Spot.getInstance().getSleepManager().disableDeepSleep();
        
        //LED status ORANGE = WAITING
        overallStatus.setColor(LEDColor.ORANGE);
        overallStatus.setOn();

        System.out.println("Connecting to Create");
        IRobotCreate irc = null;
        irc = new IRobotCreate(false);
        bot = new Robot(irc, EDemoBoard.getInstance());
        System.out.println("Create initialized");


        //LED status BLUE = INTIALIZING
        overallStatus.setColor(LEDColor.BLUE);

        //Init kernel
        Mainloop mainprocess=new Mainloop(bot);
        System.out.println("initializing userlevel");
        mainprocess.init();

        System.out.println("initializing service level");
        //Init sensor daemon
        SensorService svchost= new SensorService(bot);
        svchost.init();
        //Init telemetry daemon
        AuxService auxhost= new AuxService(bot.getSensors());

        //LED status GREEN = RUNNING
        overallStatus.setColor(LEDColor.GREEN);
        svchost.start();
        
        //Allow time for sensor hardware to initialize
        //Sensor states are populated initially
        Utils.sleep(500);

        auxhost.start();
        mainprocess.run();
                        
        /**LED status YELLOW = HALTED
         * This prevents reboot looping forever
         * and allows for diagnostics between resets
         */
        overallStatus.setColor(LEDColor.YELLOW);
        Utils.sleep(15000);        
        notifyDestroyed();                      // cause the MIDlet to exit
    }

    protected void pauseApp() {
        // This is not currently called by the Squawk VM
    }

    /**
     * Called if the MIDlet is terminated by the system.
     * I.e. if startApp throws any exception other than MIDletStateChangeException,
     * if the isolate running the MIDlet is killed with Isolate.exit(), or
     * if VM.stopVM() is called.
     * 
     * It is not called if MIDlet.notifyDestroyed() was called.
     *
     * @param unconditional If true when this method is called, the MIDlet must
     *    cleanup and release all resources. If false the MIDlet may throw
     *    MIDletStateChangeException  to indicate it does not want to be destroyed
     *    at this time.
     */
    protected void destroyApp(boolean unconditional) throws MIDletStateChangeException {
      
    }
}
